package com.mz.ozy.mvp.presenter;

import android.app.Activity;
import android.content.Context;

import com.mz.ozy.base.BasePresenterImpl;
import com.mz.ozy.bean.AssetsBean;
import com.mz.ozy.bean.MyOrderInfoBean1;
import com.mz.ozy.bean.NetBean;
import com.mz.ozy.constants.Constants;
import com.mz.ozy.mvp.model.AssetsModel;
import com.mz.ozy.mvp.view.MyOrderInfoContract1;
import com.mz.ozy.net.MyObserver;
import com.mz.ozy.net.RetrofitUtils;
import com.mz.ozy.utils.JsonUtils;
import com.mz.ozy.utils.MD5Util;
import com.mz.ozy.utils.RxUtil;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import okhttp3.ResponseBody;

/**
 * Created by Administrator on 2018/12/12.
 */

public class MyOrderInfoPresenter1 extends BasePresenterImpl<MyOrderInfoContract1.View> implements MyOrderInfoContract1.Presenter {

    @Override
    public void initData(Activity activity, String tesq, int beanType) {
        if (beanType == 0) {
            Map<String, String> maps = new HashMap<>();
            maps.put("tesq", tesq);
            RetrofitUtils.getApiService(activity).post(Constants.TransCode.TransCode10328, JsonUtils.requestJsonDate(maps), Constants.tokenId)
                    .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                    .subscribe(new MyObserver<MyOrderInfoBean1>(mView) {
                        @Override
                        public void onSuccesss(MyOrderInfoBean1 myOrderInfoBean) {

                            mView.onInitSuccess(myOrderInfoBean);
                        }

                        @Override
                        public void onError(Throwable e) {
                            super.onError(e);

                            mView.onInitError("");
                        }
                    });
        } else {
            Map<String, String> maps = new HashMap<>();
            maps.put("tesq", tesq);
            RetrofitUtils.getApiService(activity).post(Constants.TransCode.TransCode10099, JsonUtils.requestJsonDate(maps), Constants.tokenId)
                    .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                    .subscribe(new MyObserver<MyOrderInfoBean1>(mView) {
                        @Override
                        public void onSuccesss(MyOrderInfoBean1 myOrderInfoBean) {

                            mView.onInitSuccess(myOrderInfoBean);
                        }

                        @Override
                        public void onError(Throwable e) {
                            super.onError(e);

                            mView.onInitError("");
                        }
                    });
        }
    }

    @Override
    public void geAssetsInfo(Context context) {
        AssetsModel mModel = new AssetsModel();
        mModel.geAssetsInfo(context)
                .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                .subscribe(new MyObserver<AssetsBean>(mView, false) {
                    @Override
                    public void onSuccesss(AssetsBean bean) {
                        mView.geAssetsInfoSuccess(bean);
                    }
                });
    }

    @Override
    public void onPay(Context context, String orderID, String pwd, int i, int type, int beanType) {
        if (beanType == 0) {
            String url = "";
            Map<String, String> maps = new HashMap<>();
            if (type == 1) {
                url = Constants.TransCode.TransCode10059;
                maps.put("type", i + "");
            } else {
                url = Constants.TransCode.TransCode10117;
                maps.put("payType", i + "");
            }
            maps.put("id", orderID);
            maps.put("payPwd", MD5Util.getMD5String(pwd));
            RetrofitUtils.getApiService(context).post(url, JsonUtils.requestJsonDate(maps), Constants.tokenId)
                    .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                    .subscribe(new MyObserver<JSONObject>(mView, true) {

                        @Override
                        public void onSuccesss(JSONObject orderBean) {
                            mView.onPaySuccess(orderBean);
                        }

                        @Override
                        public void onError(Throwable e) {
                            super.onError(e);
                            mView.onPayError("");
                        }
                    });
        } else {
            Map<String, String> maps = new HashMap<>();
            maps.put("id", orderID);
            maps.put("payType", String.valueOf(i));
            maps.put("payPwd", MD5Util.getMD5String(pwd));
            RetrofitUtils.getApiService(context).post(Constants.TransCode.TransCode10086, JsonUtils.requestJsonDate(maps), Constants.tokenId)
                    .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                    .subscribe(new MyObserver<JSONObject>(mView, true) {

                        @Override
                        public void onSuccesss(JSONObject orderBean) {
                            mView.onPaySuccess(orderBean);
                        }

                        @Override
                        public void onError(Throwable e) {
                            super.onError(e);
                            mView.onPayError("");
                        }
                    }.setTransCode(Constants.TransCode.TransCode10086));
        }
    }

    @Override
    public void confirm(Activity activity, String tseq, int beanType) {
        if (beanType == 0) {
            Map<String, String> maps = new HashMap<>();
            maps.put("tesq", tseq);
            RetrofitUtils.getApiService(activity).post(Constants.TransCode.TransCode10045, JsonUtils.requestJsonDate(maps), Constants.tokenId)
                    .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                    .subscribe(new MyObserver<NetBean>(mView, false) {
                        @Override
                        public void onSuccesss(NetBean netBean) {

                            mView.onConfirmSuccess(netBean);
                        }

                        @Override
                        public void onError(Throwable e) {
                            super.onError(e);
                            mView.onConfirmError("");
                        }
                    });
        } else {
            Map<String, String> maps = new HashMap<>();
            maps.put("tesq", tseq);
            RetrofitUtils.getApiService(activity).post(Constants.TransCode.TransCode10304, JsonUtils.requestJsonDate(maps), Constants.tokenId)
                    .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                    .subscribe(new MyObserver<NetBean>(mView, false) {
                        @Override
                        public void onSuccesss(NetBean netBean) {

                            mView.onConfirmSuccess(netBean);
                        }

                        @Override
                        public void onError(Throwable e) {
                            super.onError(e);
                            mView.onConfirmError("");
                        }
                    });
        }
    }

    @Override
    public void cancelOrder(Activity activity, String tseq, int beanType) {
        if (beanType == 0) {
            Map<String, String> maps = new HashMap<>();
            maps.put("tesq", tseq);
            RetrofitUtils.getApiService(activity).post(Constants.TransCode.TransCode10031, JsonUtils.requestJsonDate(maps), Constants.tokenId)
                    .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                    .subscribe(new MyObserver<NetBean>(mView, false) {
                        @Override
                        public void onSuccesss(NetBean netBean) {
                            mView.onCancelOrderSuccess(netBean);
                        }

                        @Override
                        public void onError(Throwable e) {
                            super.onError(e);
                            mView.onCancelOrderError("");
                        }
                    });
        } else {
            Map<String, String> maps = new HashMap<>();
            maps.put("tesq", tseq);
            RetrofitUtils.getApiService(activity).post(Constants.TransCode.TransCode10301, JsonUtils.requestJsonDate(maps), Constants.tokenId)
                    .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                    .subscribe(new MyObserver<NetBean>(mView, false) {
                        @Override
                        public void onSuccesss(NetBean netBean) {
                            mView.onCancelOrderSuccess(netBean);
                        }

                        @Override
                        public void onError(Throwable e) {
                            super.onError(e);
                            mView.onCancelOrderError("");
                        }
                    });
        }
    }

    @Override
    public void delOrder(Activity activity, String tseq) {
        Map<String, String> maps = new HashMap<>();
//        maps.put("transCode",Constants.TransCode.DelOrderV1);
//        maps.put("t", SharePreferencesData.getSharePreferencesData(SBMallApp.ctx, Constants.PREE.TOKEN_ID));
//        maps.put("tseq", tseq);
//        RetrofitUtils.getApiService(activity).post(maps)
//                .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
//                .subscribe(new MyObserver<NetBean>(mView, false) {
//                    @Override
//                    public void onSuccesss(NetBean netBean) {
//
//                        mView.onDelOrderSuccess(netBean);
//                    }
//
//                    @Override
//                    public void onError(Throwable e) {
//                        super.onError(e);
//                        mView.onDelOrderError("");
//                    }
//                });
    }
}
